<template>
  <TnTabs
    v-model="currentTabIndex"
    height="100rpx"
    bar-width="120rpx"
    bar-color="#e5eeff"
    bg-color="#f1f5fb"
    :scroll="false"
    @change="changeTabs"
  >
    <TnTabsItem
      v-for="(item, index) in tabsData"
      :key="index"
      :title="item.text"
    />
  </TnTabs>
  <agent-list :currentTabIndex="currentTabIndex" />
  <image
    @tap.stop="toActivePage"
    class="add-png"
    mode="widthFix"
    src="@/static/imgs/add1.png"
  ></image>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { agentList } from '@/components/agent-list'
import { tnNavPage } from '@tuniao/tnui-vue3-uniapp/utils'
import { useUserStore } from '@/stores'
const currentTabIndex = ref(0)
const tabsData = [
  {
    text: '最近使用',
  },
  {
    text: '精选推荐',
  },
  {
    text: '我创建的',
  },
]
const userStore = useUserStore()
const toActivePage = async () => {
  console.log(userStore.getIfBindSn)
  if (userStore.getIfBindSn) {
    tnNavPage('/home-page/agent/add')
  } else {
    uni.showModal({
      title: '设备未激活',
      content: '学习英语需要长期坚持哦，超多权益等你来解锁',
      confirmText: '立即激活',
      cancelText: '放弃福利',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          toScanMate()
        } else if (res.cancel) {
          console.log('用户点击取消')
          tnNavPage('/pages/index/index', 'switchTab')
        }
      },
    })
  }
}
//调起微信小程序扫码
const toScanMate = () => {
  uni.scanCode({
    onlyFromCamera: true, // 默认false，true为只允许从相机扫码
    success: async (res) => {
      console.info(res) //打印出来，查看自己想要的返回结果数据
      // 拿SN码去绑定设备
      await userStore.toBindUserSn(res.result)
    },
  })
}
</script>
<style lang="scss" scoped>
.add-png {
  position: fixed;
  bottom: 80rpx;
  right: 60rpx;
  width: 100rpx;
}
</style>
